package com.jt.controller;


import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jt.service.FileService;
import com.jt.vo.EasyUIImage;

@Controller
public class FileController {
	
	/**
	 * 文件上传入门案例
	 * @param fileImage
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/file")
	public String file(MultipartFile fileImage) throws Exception{
		File dirFile=new File("D:/1-JT/jt-image");
		if (!dirFile.exists()) {
			//如果路径不存在则创建
			dirFile.mkdirs();
		}
		
		//动态获取文件名称
		String fileName=fileImage.getOriginalFilename();
		File file=new File("D:/1-JT/jt-image"+fileName);
		//开始上传文件
		fileImage.transferTo(file);
		
		return "redirect:/file.jsp";//重定向回原页面，不经过视图解析器
	}
	
	//正式图片上传
	@Autowired
	private FileService fileService;
	
	@RequestMapping("/pic/upload")
	@ResponseBody
	public EasyUIImage uploadFile(MultipartFile uploadFile) {
		return fileService.uploadFile(uploadFile);
	}
	
	
}
